package com.example.mq.mqsever.datacenter;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: WHY
 * Date: 2023-09-03
 * Time: 20:00
 */

import com.example.mq.common.MqException;
import com.example.mq.mqsever.core.Binding;
import com.example.mq.mqsever.core.Exchange;
import com.example.mq.mqsever.core.MSGQueue;
import com.example.mq.mqsever.core.Message;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

/**
 * 使用这个类来管理所有硬盘上的数据
 * 1.数据库:交换机,绑定,队列
 * 2.数据文件:消息
 * 上层逻辑如果需要操作信息,统一通过这个类来使用(上层代码不关心当前数据存储在数据库还是文件)
 */
public class DiskDataCenter {
    //这个实例用来管理数据库的数据
    private DataBaseManager dataBaseManager = new DataBaseManager();
    //这个实例用来管理文件的数据
    private MessageFileManager messageFileManager = new MessageFileManager();

    public void init() {
        //针对上述两个实例进行初始化

        dataBaseManager.init();

        messageFileManager.init();//这个方法先空着,用到再扩展
    }
    //封装交换机操作

    public void insertExchange(Exchange exchange) {
        dataBaseManager.insertExchange(exchange);

    }

    public void deleteExchange(String exchangeName) {
        dataBaseManager.deleteExchange(exchangeName);

    }

    public List<Exchange> selectAllExchanges() {
        return dataBaseManager.selectAllExchanges();
    }

    //封装队列操作
    public void insertQueue(MSGQueue queue) throws IOException {
        dataBaseManager.insertQueue(queue);
        //创建队列的同时不仅仅是把队列对象写到数据库中,还需要创建出对应的目录和文件
        messageFileManager.createQueueFiles(queue.getName());
    }

    public void deleteQueue(String queueName) throws IOException {
        dataBaseManager.deleteQueue(queueName);
        //删除队列的时候,不仅仅是把队列从数据库中删除,还需要删除对应的目录和文件
        messageFileManager.destroyQueueFiles(queueName);
    }

    public List<MSGQueue> selectAllQueues() {
        return dataBaseManager.selectAllQueues();
    }

    //封装绑定

    public void insertBinding(Binding binding) {
        dataBaseManager.insertBinding(binding);
    }

    public void deleteBinding(Binding binding) {
        dataBaseManager.deleteBiding(binding);
    }

    public List<Binding> selectAllBindings() {
        return dataBaseManager.selectAllBindings();
    }
    //封装消息
    //消息写到队列里面
    public void sendMessage(MSGQueue queue,Message message) throws IOException, MqException {
        messageFileManager.sendMessage(queue,message);
    }

    public void deleteMessage(MSGQueue queue,Message message) throws IOException, MqException, ClassNotFoundException {
        messageFileManager.deleteMessage(queue,message);
        if(messageFileManager.checkGC(queue.getName())){
            messageFileManager.gc(queue);
        }
    }

    public LinkedList<Message> loadAllMessageFromQueue(String queueName) throws IOException, MqException, ClassNotFoundException {
        return messageFileManager.loadAllMessageFromQueue(queueName);
    }


}

